<template>
  <div class="dashboard-container">
    <div class="header">
      <div class="header-information">
        <el-avatar shape="circle" :size="100" :src="require('../../assets/photo.jpg')"></el-avatar>
        <div style="margin-left: 20px;">
          <div class="title">厦门理工学院</div>
          <div class="sub-title">21软3 | 团队5</div>
        </div>
      </div>
      <div class="site-detail">
        <div class="site-detail-item" v-for="item in siteDetail">
          <div class="site-detail-item-count">{{ item.count }}</div>
          <div class="site-detail-item-title">{{ item.type }}</div>
        </div>
      </div>
    </div>

    <div class="navigation">
      <div class="navigation-item" v-for="item in navigator">
        <router-link :to="item.path">
          <img :src="require('../../assets/navigate/' + item.img)" alt="">
          <div style="margin-top: 2px;">{{ item.name }}</div>
        </router-link>
      </div>
    </div>

    <div class="bottom">
      <div class="bottom-notice">
        <el-table :data="tableData" stripe>
          <el-table-column prop="content" label="工地公告栏">
            <template slot-scope="scope">
              <div class="table-ellipsis">{{ scope.row.content }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div id="bottom-state" style="width: 300px;height:300px; margin: 30px 0 0 50px;"></div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Dashboard',
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  data() {
    return {
      uid: 0,
      siteDetail: [
        { type: '人员数量', count: 114 },
        { type: '设备数量', count: 514 },
        { type: '已开工时间', count: 1919 },
        { type: '预计竣工时间', count: 810 }
      ],
      navigator: [
        { name: '告警信息', path: '/machine/machine_situation', img: '1.png' },
        { name: '实时监控', path: '/monitor/monitor-list', img: '2.png' },
        { name: '违规检测', path: '/user/violation', img: '3.png' },
        { name: '工地人员管理', path: '/user/user-list', img: '4.png' },
        { name: '人员考勤管理', path: '/user/attendance', img: '5.png' },
        { name: '施工设备管理', path: '/machine/machine-list', img: '6.png' },
        { name: '设备位置监控', path: '/machine/machine_track', img: '7.png' },
        { name: '环境监测', path: '/environment/environment-list', img: '8.png' },
        { name: '施工安全培训', path: '/train/video-list', img: '9.png' },
        // { name: '培训内容管理', path: '#', img: '10.png' },
        // { name: '添加', path: '#', img: '11.png' },
      ],
      tableData: [],
      startDate: new Date('2021-09-05'),
      endDate: new Date('2025-06-18'),
      nowDate: new Date(),
      state: 0
    }
  },
  created() {
    //关键！！！：从浏览器缓存中获取myuser对象中的id值
    var myuser = this.$store.getters.getUser;
    this.uid = myuser.uid;
    // console.log("store:"+this.uid);
    this.randomStateNumber()
  },
  mounted() {
    //防止刷新后数据丢失
    window.addEventListener('unload', this.saveState);
    this.showStateChart()
    this.getNotice()
    this.getEmployeeCount()
    this.getDeviceCount()
    this.siteDetail[2].count = this.daysDifference(this.startDate, this.nowDate)
    this.siteDetail[3].count = this.daysDifference(this.nowDate, this.endDate)
  },
  methods: {
    getNotice() {
      var obj = this;
      this.axios({
        method: "get",
        url: "http://139.9.77.245:8081/notice"
      }).then((res) => {
        if (res.data.code == 0) {
          this.tableData = res.data.data;
        }
      })
    },
    getEmployeeCount() {
      this.axios({
        method: "get",
        url: "http://139.9.77.245:8081/employee/count"
      }).then((res) => {
        if (res.data.code == 0) {
          this.siteDetail[0].count = res.data.data;
        }
      })
    },
    getDeviceCount() {
      this.axios({
        method: "get",
        url: "http://139.9.77.245:8081/device/count"
      }).then((res) => {
        if (res.data.code == 0) {
          this.siteDetail[1].count = res.data.data;
        }
      })
    },
    saveState() {
      // 模块化后，调用 state 的代码修改为 this.$store.state.myuser
      sessionStorage.setItem('userState', JSON.stringify(this.$store.state.myuser));
    },
    daysDifference(before, after) {
      // 将日期转换为时间戳（毫秒）
      let timeDiff = after - before;
      // 将时间戳转换为天数
      let daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
      return daysDiff;
    },
    randomStateNumber() {
      this.state = Math.floor(Math.random() * 21) + 80;
    },
    showStateChart() {
      let stateChart = this.$echarts.init(document.getElementById('bottom-state'));
      stateChart.setOption({
        title: {
          text: '工地状态值',
          left: 'center',
        },
        series: [
          {
            type: 'gauge',
            progress: {
              show: true,
              width: 30
            },
            axisLine: {
              lineStyle: {
                width: 30
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              length: 15,
              lineStyle: {
                width: 2,
                color: '#999'
              }
            },
            axisLabel: {
              distance: 25,
              color: '#999',
              fontSize: 15
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 25,
              itemStyle: {
                borderWidth: 10
              }
            },
            title: {
              show: true
            },
            detail: {
              valueAnimation: true,
              fontSize: 20,
              offsetCenter: [0, '70%']
            },
            data: [
              {
                value: this.state
              }
            ]
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 10px 15px;
  width: 100%;
  height: 100%;
  background-color: #eee;
}

.header {
  background-color: #fff;
  padding: 10px 20px 10px 20px;
  border-radius: 10px;

  &-information {
    display: flex;
    align-items: center;
  }

  .title {
    font-size: 20px;
    font-weight: bold;
  }

  .sub-title {
    margin-top: 8px;
    font-size: 12px;
    color: #b6b4b4;
  }
}

.site-detail {
  display: flex;

  &-item {
    margin: 20px 125px;

    &-count {
      font-size: 20px;
      font-weight: bold;
    }

    &-title {
      font-size: 12px;
      margin-top: 5px;
      color: #b6b4b4;
    }
  }
}

.navigation {
  margin: 10px 0;
  padding: 10px 30px;
  display: flex;
  border-radius: 10px;
  background-color: #fff;

  &-item {
    margin: 0 auto;
    text-align: center;
    font-size: 14px;
  }

  img {
    width: 40px;
    height: 40px;
  }
}

.bottom {
  margin-top: 10px;
  padding: 10px 30px 0 30px;
  display: flex;
  border-radius: 10px;
  background-color: #fff;

  &-notice {
    width: 70%;
  }
}

.table-ellipsis {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>